package cn.manyufun.myf.ui.comics.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.ComicsListBean;
import cn.manyufun.myf.bean.ComicsSummary;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.common.irecyclerview.universaladapter.ViewHolderHelper;
import cn.manyufun.myf.common.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import cn.manyufun.myf.ui.comics.activity.ComicsDetailActivity;
import cn.manyufun.myf.ui.common.CommonListActivity;

import static cn.manyufun.myf.app.AppConstant.TYPE_HOT;
import static cn.manyufun.myf.app.AppConstant.TYPE_RANK;
import static cn.manyufun.myf.app.AppConstant.TYPE_RECOMMEND;

/**
 * des:新闻列表适配器
 * <p>
 * on 2016.09.10:49
 */
public class ComicsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "ComicsListAdapter";

    private ComicsListBean mData;
    private Context mContext;


    public ComicsListAdapter(Context context, ComicsListBean datas) {
        this.mContext = context;
        this.mData = datas;
    }

    public void replaceAll(ComicsListBean elements) {
        mData = elements;
        notifyDataSetChanged();
    }


    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE_RECOMMEND;
        } else if (position == 1)
            return TYPE_HOT;
        else {
            return TYPE_RANK;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_view_recommend, null);
        return new RecommendViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            RecommendViewHolder recommendViewHolder = (RecommendViewHolder) holder;
            recommendViewHolder.bindItem();
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    class RecommendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.com_recyclerView)
        RecyclerView rcy;
        @BindView(R.id.tv_type_title)
        TextView tvTypeName;
        @BindView(R.id.tv_go_all)
        TextView tvGoAll;
        int type;

        RecommendViewHolder(View view, int type) {
            super(view);
            ButterKnife.bind(this, view);
            this.type = type;

            //设置间距
            //去掉RecyclerView滑到边界产生的蓝色阴影
            rcy.setOverScrollMode(View.OVER_SCROLL_NEVER);
            if (type != TYPE_HOT) {
                int spacingInPixels = DisplayUtil.dip2px(15);
                rcy.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
            }
        }

        public void bindItem() {
            tvGoAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(CommonListActivity.EXTRA_DATA_TYPE, type);
                    CommonListActivity.startAction(mContext, bundle);
                }
            });
            if (type == TYPE_RECOMMEND) {
                tvTypeName.setText("为你推荐");
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                layoutManager.setAutoMeasureEnabled(true);
                // 设置布局管理器
                rcy.setLayoutManager(layoutManager);
//                if (rcy.getAdapter() == null) {
                    rcy.setAdapter(new GridAdapter(mContext, R.layout.item_view_recommend_grid, mData.getRecommendList(), TYPE_RECOMMEND));
//                }
            } else if (type == TYPE_HOT) {
                tvTypeName.setText("今日热门");
                GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3, LinearLayoutManager.HORIZONTAL, false);
                rcy.setLayoutManager(layoutManager);
//                if (rcy.getAdapter() == null)
                rcy.setAdapter(new GridAdapter(mContext, R.layout.common_comics_item_hot, mData.getHotList(), TYPE_HOT));

            } else if (type == TYPE_RANK) {
                tvTypeName.setText("排行榜");
//                GridLayoutManager layoutManager = new GridLayoutManager(mContext, 1, LinearLayoutManager.HORIZONTAL, false);
//                layoutManager.setAutoMeasureEnabled(true); int spacingInPixels = DisplayUtil.dip2px(15);
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                layoutManager.setAutoMeasureEnabled(true);
                rcy.setLayoutManager(layoutManager);
//                if (rcy.getAdapter() == null) {
                List<ComicsSummary> rankList = mData.getRankList();

                if (rankList != null && rankList.size() > 3) {
                    for (int i = 0; i < rankList.size(); i++) {
                        ComicsSummary cs = rankList.get(i);
                        cs.position = i + 1;
                    }
                    ComicsSummary cs = rankList.get(0);
                    rankList.remove(0);
                    rankList.add(1, cs);
                }
                rcy.setAdapter(new GridAdapter(mContext, R.layout.item_view_rank_grid, rankList, TYPE_RANK));
//                }
            }
        }
    }
//
//    class HotViewHolder extends RecyclerView.ViewHolder {
//        @BindView(R.id.com_recyclerView)
//        RecyclerView rcy;
//
//        HotViewHolder(View view) {
//            super(view);
//            ButterKnife.bind(this, view);
//        }
//
//        public void bindItem() {
//            GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3, LinearLayoutManager.HORIZONTAL, false);
////            layoutManager.setAutoMeasureEnabled(true);
//            rcy.setLayoutManager(layoutManager);
//            if (rcy.getAdapter() == null)
//                rcy.setAdapter(new GridAdapter(mContext, R.layout.common_comics_item, mData.getHotList(), TYPE_HOT));
//        }
//    }
//
//    private class RankViewHolder extends RecyclerView.ViewHolder {
//        RankViewHolder(View view) {
//            super(view);
//        }
//
//        public void bindItem() {
//        }
//    }

    private class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            if (parent.getChildAdapterPosition(view) != 0)
                outRect.left = space;
        }
    }

    private class GridAdapter extends CommonRecycleViewAdapter<ComicsSummary> {
        private int type;

        public GridAdapter(Context context, int layoutId, List<ComicsSummary> mDatas, int type) {
            super(context, layoutId, mDatas);
            this.type = type;
        }

        @Override
        public int getItemCount() {
            return mDatas == null ? 0 : mDatas.size();
        }

        @Override
        public void convert(ViewHolderHelper holder, final ComicsSummary comicsSummary) {
            if (this.type == TYPE_RECOMMEND) {
                holder.setText(R.id.tv_comics_title, comicsSummary.getTitle());
                holder.setText(R.id.tv_score, comicsSummary.getScore() + "");
                holder.setImageRoundCornerUrl(R.id.iv_comics_photo, comicsSummary.getCoverImages().get(0), DisplayUtil.dip2px(5f));
                holder.setRating(R.id.rb_score, comicsSummary.getScore(), 5);

                String type = TextUtils.join(" / ", comicsSummary.getComicType());

                holder.setText(R.id.tv_comics_type, type);
                holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(ComicsDetailActivity.FILTER, comicsSummary.getComicId());
                        ComicsDetailActivity.startAction(mContext, bundle);
                    }
                });
            } else if (this.type == TYPE_HOT) {
                holder.setText(R.id.tv_comics_title, comicsSummary.getTitle());
                holder.setText(R.id.tv_score, comicsSummary.getScore() + "");
                holder.setText(R.id.tv_author_name, comicsSummary.getAuthor());
                holder.setText(R.id.tv_update, comicsSummary.getUpdateInfo());
                holder.setText(R.id.tv_follow_num, comicsSummary.getFollowNum() + "");
                holder.setText(R.id.tv_comment_num, comicsSummary.getCommentNum() + "");
                holder.setText(R.id.tv_view_num, comicsSummary.getViewNum() + "");
                holder.setImageRoundCornerUrl(R.id.iv_comics_photo, comicsSummary.getCoverImages().get(0), DisplayUtil.dip2px(5f));
                holder.setVisible(R.id.tv_follow_str, false);
                holder.setRating(R.id.rb_score, comicsSummary.getScore(), 5);
                RelativeLayout root = holder.getView(R.id.rl_root);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(DisplayUtil.getScreenWidth(mContext) - DisplayUtil.dip2px(52), ViewGroup.LayoutParams.WRAP_CONTENT);

                root.setLayoutParams(params);
                holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(ComicsDetailActivity.FILTER, comicsSummary.getComicId());
                        ComicsDetailActivity.startAction(mContext, bundle);
                    }
                });
            } else if (this.type == TYPE_RANK) {
                int postion = getPosition(holder);
                holder.setText(R.id.tv_comics_title, comicsSummary.getTitle());
                holder.setText(R.id.tv_rank_str, comicsSummary.position + "");
                switch (postion) {
                    case 0:
                        holder.setBackgroundRes(R.id.tv_rank_str, R.drawable.btn_rank2);
                        break;
                    case 1:
                        ImageView iv = holder.getView(R.id.iv_comics_photo);
//                        iv.setLayoutParams(new CoordinatorLayout.LayoutParams((DisplayUtil.dip2px(120f), DisplayUtil.dip2px(140f)));
                        iv.getLayoutParams().height = DisplayUtil.dip2px(130f);
                        iv.getLayoutParams().width = DisplayUtil.dip2px(110f);
                        holder.setBackgroundRes(R.id.tv_rank_str, R.drawable.btn_rank1);
                        break;
                    case 2:
                        holder.setBackgroundRes(R.id.tv_rank_str, R.drawable.btn_rank3);
                        break;
                    default:
                        holder.setBackgroundRes(R.id.tv_rank_str, R.drawable.btn_rank);
                        break;
                }
                holder.setRating(R.id.rb_score, comicsSummary.getScore(), 5);
                holder.setImageRoundCornerUrl(R.id.iv_comics_photo, comicsSummary.getCoverImages().get(0), DisplayUtil.dip2px(5f));

                holder.setOnClickListener(R.id.rl_root, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Bundle bundle = new Bundle();
                        bundle.putLong(ComicsDetailActivity.FILTER, comicsSummary.getComicId());
                        ComicsDetailActivity.startAction(mContext, bundle);
                    }
                });
            }
        }

    }
}
